package reviewAlgorithms.ListNode;

public class ReverseList {
    ListNode root;
    public static void main(String[] args) {

    }
    public static ListNode reverseList1(ListNode head) {
        if (head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        ListNode newHead = head;
        ListNode currentNode = head.next;
        head.next = null;
        while (currentNode!= null){
            ListNode next = currentNode.next;
            currentNode.next = newHead;
            newHead = currentNode;
            currentNode = next;
        }
        return newHead;
    }
    public static ListNode reverseList(ListNode head) {
        if (head == null){
            return null;
        }
        ListNode[] nodes = getReverseList(head);
        return nodes[0];
    }

    private static ListNode[] getReverseList(ListNode head) {
        ListNode[] res = new ListNode[2];
        if (head.next == null) {
            res[0] = head;
            res[1] = head;
            return res;
        }
        res = getReverseList(head.next);
        res[1].next = head;
        res[1] = head;
        head.next = null;
        return res;
    }
}
